package com.sharer.last.controller;

import com.sharer.last.base.constant.CommonConstant;
import com.sharer.last.base.model.PageVO;
import com.sharer.last.base.model.R;
import com.sharer.last.interfaces.ValidateGroup;
import com.sharer.last.model.dto.friend.FriendChangeDTO;
import com.sharer.last.model.dto.friend.FriendQueryDTO;
import com.sharer.last.model.dto.friend.FriendUpdateDTO;
import com.sharer.last.model.vo.friend.FriendVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sharer.last.annotation.OperationLogger;
import com.sharer.last.service.FriendService;

import javax.annotation.Resource;

/**
 * 友链页面后台管理
 *
 * @ClassName FriendAdminController
 * @Author wangjin
 * @Date 2023/5/5 21:04
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping(CommonConstant.BLOG_ADMIN_API + "/friend")
public class FriendAdminController {
    @Resource
    FriendService friendService;

    /**
     * 获取友链页面信息
     *
     * @return
     */
    @GetMapping("/findById/{id}")
    public R<FriendVO> findById(@PathVariable Long id) {
        FriendVO friendVO = friendService.findById(id);
        return R.success(friendVO);
    }

    /**
     * 分页获取友链列表
     *
     * @param queryDTO
     * @return
     */
    @PostMapping("/page")
    public R<PageVO<FriendVO>> page(@RequestBody FriendQueryDTO queryDTO) {
        PageVO<FriendVO> pageList = friendService.selectListByPage(queryDTO);
        return R.success(pageList);
    }

    /**
     * 添加友链
     *
     * @param dto 友链DTO
     * @return
     */
    @OperationLogger("添加友链")
    @PostMapping("/create")
    public R create(@RequestBody @Validated(ValidateGroup.InsertGroup.class) FriendChangeDTO dto) {
        friendService.createFriend(dto);
        return R.handle();
    }

    /**
     * 更新友链
     *
     * @param dto 友链DTO
     * @return
     */
    @OperationLogger("更新友链")
    @PutMapping("/update")
    public R update(@RequestBody @Validated(ValidateGroup.UpdateGroup.class) FriendChangeDTO dto) {
        friendService.updateFriend(dto);
        return R.handle();
    }

    /**
     * 更新友链公开状态
     *
     * @return
     */
    @OperationLogger("更新友链公开状态")
    @PutMapping("/updatePublished")
    public R updatePublished(@RequestBody @Validated FriendUpdateDTO dto) {
        friendService.updateFriendPublishedById(dto.getId(), dto.getIsPublished());
        return R.handle();
    }

    /**
     * 按id删除友链
     *
     * @param id
     * @return
     */
    @OperationLogger("删除友链")
    @DeleteMapping("/delete")
    public R delete(@RequestParam Long id) {
        friendService.removeFriendById(id);
        return R.handle();
    }

}
